package cn.ava.basic.controller;

import cn.ava.basic.dto.LoginDto;
import cn.ava.basic.dto.WeChartDto;
import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.service.ILoginService;
import cn.ava.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: LoginController
 * @Description: 登录实现接口
 * @Author: Avavaava
 * @Date: 2022/7/26 15:28
 * @version: 23.53
 *
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * @Title: account
     * @Description: 账号登录
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:13
     * @param:
     * @return:
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            Map<String,Object> map =  loginService.accountLogin(loginDto);
            return new AjaxResult().setResultObj(map).setMessage("登录成功!");
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: phone
     * @Description: 手机号登录
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:13
     * @param:
     * @return:
     */
    @PostMapping("/phone")
    public AjaxResult phone(@RequestBody LoginDto loginDto) {
        try {
            Map<String,Object> map =  loginService.phoneLogin(loginDto);
            return new AjaxResult().setResultObj(map).setMessage("登录成功!");
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: wechat
     * @Description: 微信登录
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:13
     * @param:
     * @return:
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChartDto weChartDto) {
        try {
            return loginService.wechatLogin(weChartDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: wechatBinder
     * @Description: 微信绑定注册平台账号
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:13
     * @param:
     * @return:
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody WeChartDto weChartDto) {
        try {
            Map<String,Object> map = loginService.wechatBinder(weChartDto);
            return new AjaxResult().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



}
